<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" border :height="autoHeight">
      <el-table-column type="expand" label="能力" width="50px">
        <template slot-scope="scope">
          <el-form label-width="90px" label-position="right" style="margin: 0 24px">
            <el-form-item label="关联字典">
              <el-input v-model="scope.row.callDict" placeholder="关联数据字典" clearable></el-input>
            </el-form-item>
            <el-form-item label="关联规则">
              <el-input v-model="scope.row.callRule" placeholder="关联字段规则" clearable></el-input>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="jdbcTable" label="表名" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="jdbcName" label="列名" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="jdbcComment" label="列备注" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-model="scope.row[scope.column.property]" />
        </template>
      </el-table-column>
      <el-table-column prop="jdbcAlias" label="列别名" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-model="scope.row[scope.column.property]" />
        </template>
      </el-table-column>
      <el-table-column prop="jdbcOrder" label="列顺序" align="center">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row[scope.column.property]" controls-position="right"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="abilitiyList" label="列能力" align="center">
        <template slot-scope="scope">
          <el-select v-model="scope.row[scope.column.property]" placeholder="表能力" multiple style="width: 100%">
            <el-option v-for="item in abilities" :value="item.value" :label="item.label" :key="item.value" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="javaKey" label="关键属性" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.jdbcKey">否</span>
          <el-switch v-else v-model="scope.row[scope.column.property]"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="jdbcKey" label="主键" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.jdbcKey"type="danger">是</el-tag>
          <span v-else >否</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120px">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle @click="removeField(scope.row, scope.$index)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "ModelColumn",
  mixins: [IdeaTable],
  inject: {
    modelInfo: {
      type: 'modelInfo',
      default: function () {
        return []
      }
    },
    abilities: {
      from: 'abilities',
      default: function () {
        return []
      }
    }
  },
  watch: {
    'modelInfo.fields'(){
      this.initSource()
    }
  },
  computed: {
    autoHeight(){
      return `calc(100vh - 375px)`
    }
  },
  created() {
    this.rowKey = 'columnId'
    this.pageInfo.pageSize = 100
    this.initSource()
  },
  methods: {
    initSource() {
      this.dataSource.splice(0, this.dataSource.length)
      this.pageInfo.total = 0
      const { fields } = this.modelInfo
      if (fields && fields.length > 0) {
        this.pageInfo.total = fields.length
        fields.forEach(item => this.dataSource.push(item))
      }
    },
    removeField(row, index) {
      this.dataSource.splice(index, 1)
      this.modelInfo.fields.splice(index, 1)
      this.modelInfo.joins = this.modelInfo.joins.filter(item => {
        if (item.leftTable === row.jdbcTable && item.leftColumn === row.jdbcName) {
          return false
        } else if (item.rightTable === row.jdbcTable && item.rightColumn === row.jdbcName) {
          return false
        }
        return true
      })
    }
  }
}
</script>

<style scoped>

</style>
